package com.example.utils.oss;

import java.util.function.BooleanSupplier;

/**
 * @Description 异常返回类
 * @Author:Zhou
 * @Date:2018/7/4 15:57
 */
public class BusinessException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    private final ResultEnum resultEnum;

    private String exceptionMsg;

    public BusinessException(final ResultEnum resultEnum) {
        super("");
        this.resultEnum = resultEnum;
    }

    public BusinessException(final ResultEnum resultEnum, final String exceptionMsg) {
        super(exceptionMsg);
        this.resultEnum = resultEnum;
        this.exceptionMsg = exceptionMsg;

    }

    public int getExceptionCode() {
        return this.resultEnum.getCode();
    }

    public String getExceptionMsg() {
        return this.exceptionMsg == null ? this.resultEnum.getMsg() : this.exceptionMsg;
    }

    /**
     * 判断条件 为真抛出异常
     *
     * @param supplier     条件
     * @param resultEnum   异常编号
     * @param exceptionMsg 异常内容
     */
    public static void checkException(final ResultEnum resultEnum, final String exceptionMsg, BooleanSupplier supplier) {
        if (supplier.getAsBoolean()) {
            throw new BusinessException(resultEnum, exceptionMsg);
        }
    }

    public static void checkException400(final String exceptionMsg, BooleanSupplier supplier) {
        checkException(ResultEnum.ERROR, exceptionMsg, supplier);
    }

    public static void checkException400(BooleanSupplier supplier) {
        checkException(ResultEnum.ERROR, ResultEnum.ERROR.getMsg(), supplier);
    }

    public static void checkException400(final String exceptionMsg) {
        checkException(ResultEnum.ERROR, exceptionMsg, () -> true);
    }

}
